package crazyboyfeng.autodroid.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import crazyboyfeng.autodroid.bean.TaskBean;
import crazyboyfeng.autodroid.table.TaskTable;


public class DBManager {
	private static DBManager singleton = null;
	
	 private SQLiteDatabase wsd = null;
	 private SQLiteDatabase rsd = null;
	    
	 private DBManager() {

	 }

	 
	 public synchronized static DBManager getInstance() {

	        if (singleton == null) {
	            DBHelper databaseHelper = DBHelper.getInstance();
	            SQLiteDatabase wsd = databaseHelper.getWritableDatabase();
	            SQLiteDatabase rsd = databaseHelper.getReadableDatabase();

	            singleton = new DBManager();
	            singleton.wsd = wsd;
	            singleton.rsd = rsd;
	        }

	        return singleton;
	 }
	 
	 public void addTask(TaskBean task) {

	        ContentValues cv = new ContentValues();
	        cv.put(TaskTable.COLUMN_NAME_NAME, task.getName());
	        cv.put(TaskTable.COLUMN_NAME_DATE, task.getDate());
	        cv.put(TaskTable.COLUMN_NAME_TIME, task.getTime());
	        cv.put(TaskTable.COLUMN_NAME_TYPE, task.getType());
	        cv.put(TaskTable.COLUMN_NAME_TASK, task.getTask());
	        cv.put(TaskTable.COLUMN_NAME_STATE, task.getState());
	        cv.put(TaskTable.COLUMN_NAME_ACTIVE, task.isActive()+"");
	        wsd.insert(TaskTable.TABLE_NAME,
	        		TaskTable.COLUMN_NAME_ID, cv);
	        

	    }
	 
	 public void delTask(String id){
		 wsd.delete(TaskTable.TABLE_NAME, "_id="+id, null);
	 }
	 
	 public ArrayList<TaskBean> getAllTask(){
		 Cursor c = rsd.rawQuery("select * from "+TaskTable.TABLE_NAME, null);
		 ArrayList<TaskBean> list = new ArrayList<TaskBean>();
		 while(c.moveToNext()){
			 TaskBean bean = new TaskBean();
			 bean.setId(c.getString(0));
			 bean.setName(c.getString(1));
			 bean.setDate(c.getString(2));
			 bean.setTime(c.getString(3));
			 bean.setType(c.getString(4));
			 bean.setTask(c.getString(5));
			 bean.setState(c.getString(6));
			 bean.setActive(Boolean.parseBoolean(c.getString(7)));
			 list.add(bean);
		 }
		return list;
	 }


	public TaskBean getTask(String id){
		Cursor c = rsd.rawQuery("select * from "+TaskTable.TABLE_NAME+" where _id="+id, null);
		TaskBean bean = new TaskBean();
		if(c.moveToNext()){
			 bean.setId(c.getString(0));
			 bean.setName(c.getString(1));
			 bean.setDate(c.getString(2));
			 bean.setTime(c.getString(3));
			 bean.setType(c.getString(4));
			 bean.setTask(c.getString(5));
			 bean.setState(c.getString(6));
			 bean.setActive(Boolean.parseBoolean(c.getString(7)));
		}
		return bean;
	}
	public void editTask(TaskBean tb) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
        cv.put(TaskTable.COLUMN_NAME_NAME, tb.getName());
        cv.put(TaskTable.COLUMN_NAME_DATE, tb.getDate());
        cv.put(TaskTable.COLUMN_NAME_TIME, tb.getTime());
        cv.put(TaskTable.COLUMN_NAME_TYPE, tb.getType());
        cv.put(TaskTable.COLUMN_NAME_TASK, tb.getTask());
        cv.put(TaskTable.COLUMN_NAME_STATE, tb.getState());
		wsd.update(TaskTable.TABLE_NAME, cv, "_id="+tb.getId(), null);
	}
	
	public void setTaskActive(String id,boolean active){
		ContentValues cv = new ContentValues();
		cv.put(TaskTable.COLUMN_NAME_ACTIVE, active+"");
		wsd.update(TaskTable.TABLE_NAME, cv, "_id="+id, null);
	}
}
